package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.service.MarketAddressService;
import com.cskaoyan.market.service.MarketAddressServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/6 17:26
 */
@WebServlet("/admin/address/*")
public class AdminAddressController extends HttpServlet {
    MarketAddressService marketAddressService=new MarketAddressServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/address/", "");
        System.out.println(op);
        if ("list".equals(op)) {
            list(req, resp);
        }


    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sortParam = req.getParameter("sort");
        String orderParam = req.getParameter("order");
        String nameParam = req.getParameter("name");
        String userIdParam = req.getParameter("userId");

        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(object);
            resp.getWriter().println(s);
            return;
        }
        List<MarketAddress> marketAddressList=marketAddressService.list(limit,page,sortParam,orderParam,nameParam,userIdParam);
        Object o=ResponseUtil.okList(marketAddressList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }
}
